package com.share.order.rest;

import com.share.order.api.OrderApi;
import com.share.order.bean.Order;
import com.share.user.api.UserApi;
import com.share.user.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;

/**
 * author:caifan
 * date:2019/4/28
 */
@RestController
public class OrderRest implements OrderApi {
    @Autowired
    UserApi userApi;

    @Override
    public Order getOrderByUserId(@RequestParam("userId")Long userId) {
        Order order = new Order();
        order.setUserId(userId);
        return order;
    }

    @Override
    public Order getBirth(@RequestParam("birth") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date birth) {
        Order order = new Order();
        User user = userApi.getUserByBirth(birth);
        order.setBirth(user.getBirth());
        return order;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
    }

}
